<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class IndexController extends Controller
{
    //index
    public function index()
    {

        //首页统计信息
        $userCount = DB::table('users') -> count();

    	return view('admin.index.index', ['userCount' => $userCount]);
    }
    //用户瀑布
    public function ajaxmore()
    {
        $data = DB::table('userdetail') -> join('users', 'users.id', '=', 'userdetail.id') -> select('userdetail.*', 'users.ctime') -> orderBy('ctime', 'desc') -> paginate(8);
        $arr = array();
        $a = array();
        $i = 0;
        foreach($data as $v)
        {
            
            $arr[$i]= $v;

            foreach($arr[$i] as $k => $v)
            {
                $a[$i][$k] = $v;
            }

            $a[$i]['ctime'] = date('Y-m-d', $a[$i]['ctime']);

            $i++;
        }


        //转换成json
        $jsonData = json_encode($a);
        return $jsonData;
    }
    
    //影评瀑布
    public function ajaxpost()
    {
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $start = ($p-1) * 8;
        //关联查询
        $data = DB::select("select post.*,types.name,userdetail.name as uname,types.id as tid from post,types,userdetail where types.id=post.pid and post.uid=userdetail.id order by ctime desc limit ?,8", [$start]);
        $arr = array();
        $a = array();
        $i = 0;
        foreach($data as $v)
        {
            
            $arr[$i]= $v;

            foreach($arr[$i] as $k => $v)
            {
                $a[$i][$k] = $v;
            }

            $i++;
        }
        //转换成json
        $jsonDat = json_encode($a);

         return $jsonDat;
    }
    //电影瀑布
    public function ajaxtypes()
    {
        $p = empty($_GET['g']) ? 1 : $_GET['g'];
        $start = ($p-1) * 5;
        //关联查询
        $data = DB::select("select * from types order by id desc limit ?,5", [$start]);
        $arr = array();
        $a = array();
        $i = 0;
        $se = array();
        $ew = array();
        foreach($data as $ka => $va)
        {
            $se[$ka] = explode('|',$va -> cast);
            foreach ($se[$ka] as $key => $value)
            {
                $ve = '';
                $ew[$key] = explode(',', $value);
                foreach($ew as $kkkk => $vvvv)
                {
                    $ve .=$ew[$kkkk][0].'<br>';
                }
                $data[$ka] -> cast = $ve;
            }
            
        }


        foreach($data as $v)
        {
            
            $arr[$i]= $v;

            foreach($arr[$i] as $k => $v)
            {
                $a[$i][$k] = $v;
            }

            $i++;
        }
        //转换成json
        $jsonDat = json_encode($a);

         return $jsonDat;
    }
}
